<?php

include_once 'Node.php';
include_once 'IterativeInterpreter.php';

class HTMLInterpreter extends IterativeInterpreter{
	
	var $htmlTokens = array('div', 'span', 'ul', 'hr', 'canvas');
	var $htmlAttrType = array('id','style', 'class');
	var $specialAttr = array(
		'canvas' => array('width', 'height')
	);
		
	public function interpret(Node $node){
		$name = $node->getAttribute('#');
				
		$ret = '';
		if($name == 'html'){
			return $node->getAttribute('[]');
		}else if(in_array($name, $this->htmlTokens)){
			$ret .= '<'.$name;
			foreach ($this->htmlAttrType as $htmlAttr){
				if($node->hasAttribute($htmlAttr)){
					$v = $node->getAttribute($htmlAttr);
					$ret .= ' '.$htmlAttr.'="'.$v.'"';		
				}
			}
			if(in_array($name, $this->specialAttr)){
				$atAttrs = $node->getAtAttributes();
				$i = 0;
				foreach($this->specialAttr[$name] as $attr){
					$ret .= ' '.$attr.'="'.$atAttrs[$i++].'"';
				}
			}
			$ret .= '>';
			$ret .= $this->iterate($node);
			$ret .= '</'.$name.'>';
		}
		
		return $ret;
	}
	
	public function iterate(Node $node){
		return parent::interpret($node);
	}	
}